﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace OrderManager
{
    internal class OrderService
    {
        List<Order> orders = new List<Order>();
        public void AddOrder()
        {
            Customer customer = new Customer();
            Console.WriteLine("Please input customer Name:");
            string cusName = Console.ReadLine();
            customer.CustomerName = cusName;

            Stuff stuff = new Stuff();
            Console.WriteLine("Please input stuff Type:");
            string stuType = Console.ReadLine();
            stuff.StuffType = stuType;
            Console.WriteLine("stuff total price");
            double stuprice = Convert.ToDouble(Console.ReadLine());
            stuff.TotalPrice = stuprice;

            Order order = new Order();
            Console.WriteLine("Please input the order ID:");
            int id = Convert.ToInt32(Console.ReadLine());
            order.Id = id;
            order.stuff = stuff;
            order.customer = customer;
            bool flag = true;

            foreach (Order item in orders)
            {
                flag = order.Equals(item);
                //Console.WriteLine(flag);
                if (flag)
                {
                    Console.WriteLine("Fail added.Repeated order");
                    Console.ReadKey();
                    Console.Clear();
                    return;
                }
            }
            orders.Add(order);
            Console.WriteLine("Successfully Add Order");
            Console.ReadKey();
            Console.Clear();
        }
        public void DelOrder()
        {
            Console.WriteLine("Please choose the inquiry method");
            Console.WriteLine("1.order ID");
            Console.WriteLine("2.customer name");
            Console.WriteLine("3.stuff total price");
            Console.WriteLine("4.stuff type");
            int choice = Convert.ToInt32(Console.ReadLine());
            //bool flag = false;
            //Order ord;
            switch (choice)
            {
                case 1:
                    {
                        Console.WriteLine("Please input the order ID:");
                        int id = Convert.ToInt32(Console.ReadLine());
                        for (int i = 0; i < orders.Count; i++)
                            if (orders[i].Id == id)
                            {
                                Console.WriteLine(orders[i].ToString());
                                Console.WriteLine("Delete? y/n");
                                char c = Convert.ToChar(Console.ReadLine());
                                switch (c)
                                {
                                    case 'y':
                                        {
                                            orders.Remove(orders[i]);
                                            Console.WriteLine("successfully delete");
                                            goto END;
                                        }
                                    case 'n': goto END;
                                    default: goto END;
                                }

                            }
                        Console.WriteLine("order doen't exist");
                        break;
                    }
                case 2:
                    {
                        Console.WriteLine("Please input the customer name:");
                        string name = Console.ReadLine();
                        for (int i = 0; i < orders.Count; i++)
                            if (orders[i].customer.CustomerName == name)

                            {
                                Console.WriteLine(orders[i].ToString());
                                Console.WriteLine("Delete? y/n");
                                char c = Convert.ToChar(Console.ReadLine());
                                switch (c)
                                {
                                    case 'y':
                                        {
                                            orders.Remove(orders[i]);
                                            Console.WriteLine("successfully delete");
                                            goto END;

                                        }
                                    case 'n': goto END;
                                    default: goto END;
                                }

                            }
                        Console.WriteLine("order doen't exist");
                        break;
                    }
                case 3:
                    {
                        Console.WriteLine("Please input the price:");
                        double price = Convert.ToDouble(Console.ReadLine());
                        for (int i = 0; i < orders.Count; i++)
                            if (orders[i].stuff.TotalPrice == price)
                            {
                                Console.WriteLine(orders[i].ToString());
                                Console.WriteLine("Delete? y/n");
                                char c = Convert.ToChar(Console.ReadLine());
                                switch (c)
                                {
                                    case 'y':
                                        {
                                            orders.Remove(orders[i]);
                                            Console.WriteLine("successfully delete");
                                            goto END;

                                        }
                                    case 'n': goto END;
                                    default: goto END;
                                }

                            }
                        Console.WriteLine("order doen't exist");
                        break;
                    }
                case 4:
                    {
                        Console.WriteLine("Please input the stuff type:");
                        string stutype = Console.ReadLine();
                        for (int i = 0; i < orders.Count; i++)
                            if (orders[i].stuff.StuffType == stutype)
                            {
                                Console.WriteLine(orders[i].ToString());
                                Console.WriteLine("Delete? y/n");
                                char c = Convert.ToChar(Console.ReadLine());
                                switch (c)
                                {
                                    case 'y':
                                        {
                                            orders.Remove(orders[i]);
                                            Console.WriteLine("successfully delete");
                                            goto END;

                                        }
                                    case 'n': goto END;
                                    default: goto END;
                                }

                            }
                        Console.WriteLine("order doen't exist");
                        break;
                    }
                default: break;
            }
        END: Console.ReadKey();
            Console.Clear();
        }
        public void InqOrder()
        {
            Console.WriteLine("Please choose the inquiry method");
            Console.WriteLine("1.order ID");
            Console.WriteLine("2.customer name");
            Console.WriteLine("3.stuff total price");
            Console.WriteLine("4.stuff type");
            int choice = Convert.ToInt32(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    {
                        Console.WriteLine("Please input the order ID:");
                        int id = Convert.ToInt32(Console.ReadLine());
                        var query = from o in orders where o.Id == id orderby o.stuff.TotalPrice select o;
                        List<Order> order1 = query.ToList();
                        foreach (Order o in order1)
                            Console.WriteLine(o.ToString());
                        break;
                    }
                case 2:
                    {
                        Console.WriteLine("Please input the customer name:");
                        string name = Console.ReadLine();
                        var query = from o in orders where o.customer.CustomerName == name orderby o.stuff.TotalPrice select o;
                        List<Order> order1 = query.ToList();
                        foreach (Order o in order1)
                            Console.WriteLine(o.ToString());
                        break;
                    }
                case 3:
                    {
                        Console.WriteLine("Please input the price:");
                        double price = Convert.ToDouble(Console.ReadLine());
                        var query = from o in orders where o.stuff.TotalPrice == price orderby o.stuff.TotalPrice select o;
                        List<Order> order1 = query.ToList();
                        foreach (Order o in order1)
                            Console.WriteLine(o.ToString());
                        break;
                    }
                case 4:
                    {
                        Console.WriteLine("Please input the stuff type:");
                        string stutype = Console.ReadLine();
                        var query = from o in orders where o.stuff.StuffType == stutype orderby o.stuff.TotalPrice select o;
                        List<Order> order1 = query.ToList();
                        foreach (Order o in order1)
                            Console.WriteLine(o.ToString());
                        break;
                    }
                default: break;

            }
            Console.ReadKey();
            Console.Clear();
        }
        public void EditOrder()
        {
            Console.WriteLine("Please input the order Id you want to edit");
            int ordID = Convert.ToInt32(Console.ReadLine());
            var query = from o in orders where o.Id == ordID select o;
            List<Order> ord = query.ToList();
            if (ord.Count() > 0)
            {

                Console.WriteLine("Find the target order:");
                foreach (Order o in ord)
                    Console.WriteLine(o.ToString());
            }
            else
            {
                Console.WriteLine("Order does not exist");
                return;
            }


            Console.WriteLine("Please choose the edit item:");
            Console.WriteLine("1.order ID");
            Console.WriteLine("2.customer name");
            Console.WriteLine("3.stuff total price");
            Console.WriteLine("4.stuff type");
            int choice = Convert.ToInt32(Console.ReadLine());
            switch (choice)
            {
                case 1:
                    {
                        Console.WriteLine("Please input the updated order ID:");
                        int id = Convert.ToInt32(Console.ReadLine());
                        ord[0].Id = id;
                        Console.WriteLine("updated order:" + ord[0].ToString());
                        break;
                    }
                case 2:
                    {
                        Console.WriteLine("Please input the customer name:");
                        string name = Console.ReadLine();
                        ord[0].customer.CustomerName = name;
                        Console.WriteLine("updated order:" + ord[0].ToString());
                        break;
                    }
                case 3:
                    {
                        Console.WriteLine("Please input the price:");
                        double price = Convert.ToDouble(Console.ReadLine());
                        ord[0].stuff.TotalPrice = price;
                        Console.WriteLine("updated order:" + ord[0].ToString());
                        break;
                    }
                case 4:
                    {
                        Console.WriteLine("Please input the stuff type:");
                        string stutype = Console.ReadLine();
                        ord[0].stuff.StuffType = stutype;
                        Console.WriteLine("updated order:" + ord[0].ToString());
                        break;
                    }
                default: break;

            }
            Console.ReadKey();
            Console.Clear();
        }

        public void SortOrder()
        {
            Console.WriteLine("orders sorted by order id:");
            var query = from o in orders orderby o.Id select o;
            List<Order> ord = query.ToList();
            foreach(Order order in ord)
                Console.WriteLine(order.ToString());

            Console.WriteLine("orders sorted by total price");
            var query1 = from o in orders orderby o.stuff.TotalPrice select o;
            List<Order> ord1 = query1.ToList();
            foreach (Order order in ord1)
                Console.WriteLine(order.ToString());

            Console.ReadKey();
            Console.Clear();
        }

    }
}
